package com.istock.union.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.istock.base.common.api.model.PageInfo;
import com.istock.base.enumration.Enums;
import com.istock.union.user.dao.CredentialRoleInfoMapper;
import com.istock.union.user.dao.UserCredentialInfoMapper;
import com.istock.union.user.dao.ext.ExtUserCredentialInfoMapper;
import com.istock.union.user.model.CredentialEditVO;
import com.istock.union.user.model.CredentialRoleInfo;
import com.istock.union.user.model.CredentialRoleInfoExample;
import com.istock.union.user.model.RoleInfo;
import com.istock.union.user.model.UserCredentialInfo;
import com.istock.union.user.model.UserCredentialInfoExample;
import com.istock.union.user.model.UserCredentialVO;
import com.istock.union.user.service.RoleInfoService;
import com.istock.union.user.service.UserCredentialInfoService;

@Repository
public class UserCredentialInfoServiceImpl implements UserCredentialInfoService {

	@Autowired
	private UserCredentialInfoMapper userCredentialInfoMapper;
	
	@Autowired
	private CredentialRoleInfoMapper credentialRoleMapper;
	
	@Autowired
	private ExtUserCredentialInfoMapper extUserCredentialMapper;
	
	@Autowired
	private RoleInfoService roleInfoService;
	
	@Override
	public List<UserCredentialInfo> findUserCredentialList(String userId) {
		UserCredentialInfoExample example = new UserCredentialInfoExample();
		UserCredentialInfoExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		return userCredentialInfoMapper.selectByExample(example);
	}

	@Override
	public UserCredentialInfo findUserBasicCredential(String userId) {
		UserCredentialInfoExample example = new UserCredentialInfoExample();
		UserCredentialInfoExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andTypeEqualTo(Enums.getInstance().getEnumsValueByCode("CREDENTIAL_TYPE", "CREDENTIAL_TYPE_OWNER"));
		
		List<UserCredentialInfo> list = userCredentialInfoMapper.selectByExample(example);
		if(list != null && list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
	

	@Override
	@Transactional
	public void saveUserCredential(CredentialEditVO credentialInfo) {
		if(credentialInfo != null) {
			String credentialId = credentialInfo.getId();
			if(StringUtils.isNotBlank(credentialId)) {
				//modify
				CredentialRoleInfoExample example = new CredentialRoleInfoExample();
				CredentialRoleInfoExample.Criteria criteria = example.createCriteria();
				criteria.andCredentialIdEqualTo(credentialId);
				credentialRoleMapper.deleteByExample(example);
				
				UserCredentialInfo info = new UserCredentialInfo();
				info.setDeptCode(credentialInfo.getDeptCode());
				info.setType(credentialInfo.getType());
				info.setUserId(credentialInfo.getUserId());
				info.setId(credentialInfo.getId());
				userCredentialInfoMapper.updateByPrimaryKeySelective(info);
			}else {
				UserCredentialInfo info = new UserCredentialInfo();
				info.setDeptCode(credentialInfo.getDeptCode());
				info.setType(credentialInfo.getType());
				info.setUserId(credentialInfo.getUserId());
				userCredentialInfoMapper.insertSelective(info);
				credentialId = info.getId();
			}
			
			if(StringUtils.isNotBlank(credentialInfo.getRoleIds())) {
				String[] roleIdArray = credentialInfo.getRoleIds().split(",");
				if(roleIdArray != null && roleIdArray.length>0) {
					for(String roleId : roleIdArray) {
						CredentialRoleInfo info = new CredentialRoleInfo();
						info.setRoleId(roleId);
						info.setCredentialId(credentialId);
						credentialRoleMapper.insertSelective(info);
					}
				}
			}
		}
	}

	@Override
	public void removeUserCredential(List<String> idList) {
		UserCredentialInfoExample example = new UserCredentialInfoExample();
		example.createCriteria().andIdIn(idList);
		userCredentialInfoMapper.deleteByExample(example);
	}

	@Override
	public List<UserCredentialVO> queryUserCredentialByLoginName(String loginName,PageInfo pageInfo) {
		Map<String , Object> paramMap = new HashMap<String , Object>();
		if(StringUtils.isNotBlank(loginName)) {
			paramMap.put("loginName", "%"+loginName + "%");
		}
		
		List<UserCredentialInfo> credentialList = extUserCredentialMapper.queryUserCredentialByLoginName(paramMap, pageInfo);
		
		if(credentialList != null) {
			List<UserCredentialVO> result = new ArrayList<UserCredentialVO>();
			for(UserCredentialInfo info : credentialList) {
				UserCredentialVO item = new UserCredentialVO();
				BeanUtils.copyProperties(info, item);
								
				List<RoleInfo> roleList = roleInfoService.selectCredentialRole(info.getId());
				StringBuffer sb = new StringBuffer();
				for(RoleInfo roleInfo : roleList) {
					sb.append(roleInfo.getRoleName()).append(",");
				}
				item.setRoleList(sb.toString());
				result.add(item);
			}
			pageInfo.setData(result);
			return result;
		}
		return null;
	}

	@Override
	public List<UserCredentialVO> findUserCredentialByUserId(String userId) {
		List<UserCredentialInfo> credentialList = findUserCredentialList(userId);
		if(credentialList != null) {
			List<UserCredentialVO> result = new ArrayList<UserCredentialVO>();
			for(UserCredentialInfo info : credentialList) {
				UserCredentialVO item = new UserCredentialVO();
				BeanUtils.copyProperties(info, item);
								
				List<RoleInfo> roleList = roleInfoService.selectCredentialRole(info.getId());
				StringBuffer sb = new StringBuffer();
				for(RoleInfo roleInfo : roleList) {
					sb.append(roleInfo.getRoleName()).append(",");
				}
				item.setRoleList(sb.toString());
				result.add(item);
			}
			return result;
		}
		return null;
	}

	@Override
	public UserCredentialInfo findCredentialById(String credentialId) {
		return userCredentialInfoMapper.selectByPrimaryKey(credentialId);
	}

}
